package com.gitee.cheungming.goods.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gitee.cheungming.common.Result;
import com.gitee.cheungming.goods.config.DiscountGoods;
import com.gitee.cheungming.goods.config.SecondKill;
import com.gitee.cheungming.goods.model.Goods;
import com.gitee.cheungming.goods.service.IGoodsService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Zhang Ming
 * @since 2025-10-28
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    IGoodsService goodsService;

    @Value("${server.port}")
    private String port;

    @Resource
    private SecondKill secondKill;

    @Resource
    private DiscountGoods discountGoods;

    @RequestMapping("/list")
    public Result<List<Goods>> list() {
        PageDTO<Goods> page = new PageDTO<>(1, 10);
        return Result.success(goodsService.list(page));
    }

    @RequestMapping("/reduceStock")
    public Result<?> reduceStock(Long id, Integer count) {
        return Result.success(goodsService.reduceStock(id, count));
    }

    @RequestMapping("/testLoadBalance")
    public Result<?> testLoadBalance() {
        return Result.success("port:" + port);
    }


    @RequestMapping("/secondKill")
    public Result<?> secondKill() {
        return Result.success(JSONUtil.toJsonStr(secondKill));
    }

    @RequestMapping("/getDiscountGoods")
    public Result<?> getDiscountGoods() {
        return Result.success(JSONUtil.toJsonStr(discountGoods));
    }
}
